<?php
error_reporting( E_ALL );
ini_set( 'display_errors', '1' );
//将出错信息输出到一个文本文件
ini_set( 'error_log', dirname( __DIR__ ) . '/logs/php_error_log.txt' );

//定义常量 APP_ROOT web root
define( 'APP_ROOT', dirname( __DIR__ ) . '/' );
define( 'APP_MODULE', dirname( __DIR__ ) . '/apps/' );
define( 'APP_MODE', 'dev' );//dev 

$di = new \Phalcon\DI\FactoryDefault();

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */

$loader->registerNamespaces( array(
	'libraries' => APP_ROOT . 'libraries/',
	'utils'		=> APP_ROOT . 'apps/utils/',
	'apis'		=> APP_ROOT . 'apis',
	'enums'		=> APP_ROOT . 'enums/',
	'listeners'	=> APP_ROOT . 'listeners/',
    'vendors'   => APP_ROOT . 'vendors/'
));

$loader->register();

//Registering config
$arrInitCfg = array(
		array( 'name' => 'lockCfg', 'file' => 'lock.php', 'inDi' => true ),
		array( 'name' => 'sessionCfg', 'file' => 'session.php', 'inDi' => true ),
		array( 'name' => 'urlCfg', 'file' => 'url.php', 'inDi' => true ),
		array( 'name' => 'dbCfg', 'file' => 'db.php', 'inDi' => true ),
		array( 'name' => 'queueCfg', 'file' => 'queue.php', 'inDi' => true ),
		array( 'name' => 'config', 'file' => 'config.php', 'inDi' => true ),
		array( 'name' => 'logCfg', 'file' => 'log.php', 'inDi' => true ),
		array( 'name' => 'cacheCfg', 'file' => 'cache.php', 'inDi' => true ),
		array( 'name' => 'emailCfg', 'file' => 'email.php', 'inDi' => true ),
);

// $iCfgLen = count( $arrInitCfg );
//set in di
foreach( $arrInitCfg as $cfg )
{
	if( file_exists( APP_ROOT . 'config/' . APP_MODE . '/' . $cfg[ 'file' ] ))
	{
		if( $cfg[ 'inDi' ] )
		{
			$di->set( $cfg['name'], require APP_ROOT . 'config/' . APP_MODE . '/' . $cfg['file'], true );
		}
		else
		{
			${$cfg[ 'name' ]} = require APP_ROOT . 'config/' . APP_MODE . '/' . $cfg['file'];
		}
	}
	else
	{
		if( $cfg[ 'inDi' ] )
		{
			$di->set( $cfg['name'], require APP_ROOT . 'config/' . $cfg['file'], true );
		}
		else
		{
			${$cfg[ 'name' ]} = require APP_ROOT . 'config/' . $cfg['file'];
		}
	}
}


$app = new \Phalcon\Mvc\Application($di);
try
{
	require APP_ROOT . 'config/services.php';//加载公共services
	
	// Register the installed modules
	$app->registerModules( array(
	        'common' => 
			array(
	            'className' => 'apps\common\Module',
	            'path' => APP_ROOT . 'apps/common/Module.php'
	        ),
// 	        'app' => 
// 	        array(
// 	    		'className' => 'apps\app\Module',
// 	    		'path' => APP_ROOT . 'apps/app/Module.php'
// 	        ),
	        'fzq' =>
	        array(
	            'className' => 'apps\fzq\Module',
	            'path' => APP_ROOT . 'apps/fzq/Module.php'
	        ),
	));


    echo $app->handle()->getContent();
}
catch( \Exception $e )
{
	echo $e->getTraceAsString();
}
